<template>
  <el-card>
    <el-row>
      <el-select style="margin-right: 20px" size="small" v-model="selectedCourseId" placeholder="请选择">
        <el-option
          v-for="item in teacherCourseList"
          :key="item.courseId"
          :label="item.courseName"
          :value="item.courseId">
        </el-option>
      </el-select>
      <el-button type="primary" size="small" @click="getTeacherExperimentList()">查询</el-button>
    </el-row>
    <el-row>
      <!--  展示老师选择的实验  -->
      <el-table  :data="teacherExperimentList" border stripe>
        <el-table-column type="index" label="序号"></el-table-column>
<!--        <el-table-column prop="experId" label="实验编码" width="180"></el-table-column>-->
        <el-table-column prop="experName" label="实验名称"></el-table-column>
        <el-table-column prop="startTime" label="开始时间"></el-table-column>
        <el-table-column prop="endTime" label="截止时间"></el-table-column>
        <!--scope相当于是tableData的一行，与el-table-column唯一对应-->
        <el-table-column prop="difficuty" label="实验难度" >
          <template v-slot:default="scope">
            <div v-if="scope.row.difficuty === 1">
              <el-tag type="success">基础</el-tag>
            </div>
            <div v-if="scope.row.difficuty === 2">
              <el-tag>提高</el-tag>
            </div>
            <div v-if="scope.row.difficuty === 3">
              <el-tag type="warning">综合</el-tag>
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="state" label="状态">
          <template v-slot:default="scope">
            <div v-if="scope.row.state === null">
              <el-tag type="info">未选择</el-tag>
            </div>
            <div v-if="scope.row.state === 1">
              <el-tag type="success">已选择</el-tag>
            </div>
          </template>
        </el-table-column>
        <el-table-column fixed="right" label="操作">
          <template slot-scope="scope">
            <el-tooltip effect="dark" content="选择" placement="top" :enterable="false">
              <el-button @click="chooseExp(scope.row)" type="primary"
                         icon="el-icon-plus" size="small"></el-button>
            </el-tooltip>
            <el-tooltip effect="dark" content="取消" placement="top"
                        :enterable="false">
                <el-button @click="deleteExp(scope.row.experId)" type="danger"
                         icon="el-icon-delete"
                         size="small"></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>
    </el-row>
    <!--选择实验对话框-->
    <el-dialog title="选择实验" :visible.sync="newExpFormVisible" @close="chooseExpClosed">
      <el-form ref="newExpForm" :model="newExpForm">
        <el-form-item prop="courseId" size="small" label="课程名称" label-width="100px">
         <el-input v-model="this.CurrentCourseName" :disabled="true" ></el-input>
        </el-form-item>
        <el-form-item prop="experId" size="small" label="实验名称" label-width="100px">
          <el-input v-model="newExpForm.experName" :disabled="true" ></el-input>
        </el-form-item>
        <el-form-item prop="startTime" size="small" label="开始时间" label-width="100px">
          <el-date-picker
            v-model="newExpForm.startTime"
            type="date"
            placeholder="选择开始时间"
            format="yyyy 年 MM 月 dd 日"
            value-format="yyyy-MM-dd">
          </el-date-picker>
        </el-form-item>
        <el-form-item prop="endTime" size="small" label="截止时间" label-width="100px">
          <el-date-picker
            v-model="newExpForm.endTime"
            type="date"
            placeholder="选择截止时间"
            format="yyyy 年 MM 月 dd 日"
            value-format="yyyy-MM-dd">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="newExpFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitChooseExp">确 定</el-button>
      </div>
    </el-dialog>
  </el-card>
</template>

<script>
export default {
  data () {
    return {
      teacherCourseList: [],
      courseExperimentList: [],
      teacherExperimentList: [],
      // CurrentSel: '',
      CurrentCourseName: '',
      selectedCourseId: '',
      obj: '',
      newExpForm: {
        courseId: '',
        experId: '',
        startTime: '',
        endTime: ''
      },
      newExpFormVisible: false,
      editExpForm: {
        courseId: '',
        experId: '',
        startTime: '',
        endTime: ''
      },
      editExpFormVisible: false
    }
  },
  async created () {
    await this.getTeacherCourseList()
    await this.getTeacherExperimentList()
  },
  // computed: {
  //   // 使用计算属性 过滤数组数据
  //   // 也可以直接在getData中使用 filter 过滤
  //   courseExperimentListById: function () {
  //     return this.courseExperimentList.filter((data) => {
  //       return data.courseId === this.CurrentSel
  //     })
  //   }
  // },
  methods: {
    async getTeacherCourseList () {
      const { data: res } = await this.$http.post('/getTeacherCourse')
        .catch((error) => {
          console.log(error)
          this.$message.error('网络连接超时')
        })
      if (res.status !== 200) this.$message.error('获取课程信息失败')
      console.log(res.data.data)
      const teacherCourseList = res.data
      this.teacherCourseList = teacherCourseList.data
      this.total = teacherCourseList.total
      this.selectedCourseId = this.teacherCourseList[0].courseId
    },
    // async getCourseExperiment () {
    // },
    // 由选中的课程获取课程对应的课程。
    // currentSel (selVal) {
    //   this.CurrentSel = selVal
    // },
    // 获取老师选择的实验信息
    async getTeacherExperimentList () {
      const { data: res } = await this.$http.post('/queryTeacherExperiment', { courseId: this.selectedCourseId })
        .catch((error) => {
          console.log(error)
          this.$message.error('网络连接超时')
        })
      if (res.status !== 200) this.$message.error('获取实验信息失败,' + res.data.msg)
      console.log(res)
      const teacherExperimentList = res.data
      this.teacherExperimentList = teacherExperimentList.data
    },
    chooseExp (row) {
      this.newExpFormVisible = true
      // 获取selectCourseId对应的course名称
      this.obj = this.teacherCourseList.find((item) => {
        return item.courseId === this.selectedCourseId
      })
      // 获取选中的课程名字
      this.CurrentCourseName = this.obj.courseName
      this.newExpForm = row
    },
    submitChooseExp () {
      console.log(this.newExpForm)
      this.$refs.newExpForm.validate(async valid => {
        if (!valid) return
        const { data: res } = await this.$http.post('/chooseExperiment', this.newExpForm)
          .catch((error) => {
            console.log(error)
          })
        if (res.status !== 200) {
          this.$message.error('选择实验失败,' + res.data.msg)
        } else {
          this.$message.success('选择实验成功')
          this.newExpFormVisible = false
          await this.getTeacherExperimentList()
        }
      })
    },
    chooseExpClosed () {
      this.$refs.newExpForm.resetFields()
    },
    editExp (row) {
      this.editExpFormVisible = true
      // console.log(row)
      this.editExpForm = row
    },
    submitEditExp () {
      // console.log(this.editCourseForm)
      this.$refs.editCourseForm.validate(async valid => {
        if (!valid) return
        const { data: res } = await this.$http.post('/', this.editCourseForm)
          .catch((error) => {
            console.log(error)
          })
        if (res.status !== 200) {
          this.$message.error('更新实验失败,' + res.data.msg)
        } else {
          this.$message.success('更新实验成功')
          this.editCourseFormVisible = false
          await this.getTeacherExperimentList()
        }
      })
    },
    async deleteExp (Id) {
      console.log(Id)
      const flag = await this.$confirm('确认要取消吗？')
      if (flag) {
        const { data: res } = await this.$http.post('/deleteAppliedExp', { experId: Id })
          .catch((error) => {
            console.log(error)
            this.$message.error('取消实验失败,请检查网络')
          })
        if (res.status === 200) {
          this.$message.success('取消实验成功')
          await this.getTeacherExperimentList()
        } else {
          this.$message.error('取消实验失败,请检查网络')
        }
      }
    }
  }
}
</script>
